﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using CoreModel = PlayerScannerCore.Model;
using Mvvm;
using PlayerScannerCore.Model;

namespace PlayerScannerCore.ViewModel
{
    public class PlayerInevntoryViewModel : ViewModelBase
    {
        private CoreModel.Model _model;
        private PlayerViewModel _owner;
        private ObservableCollection<ItemViewModel> _inventory = new ObservableCollection<ItemViewModel>();
        private int _meseta;

        public PlayerInevntoryViewModel(CoreModel.Model model, PlayerViewModel owner) { _model = model; _owner = owner; }

        public ObservableCollection<ItemViewModel> Inventory
        {
            get { return _inventory; }
            set
            {
                if (_inventory != value)
                {
                    _inventory = value;
                    OnPropertyChanged("Inventory");
                }
            }
        }

        public int Meseta
        {
            get { return _meseta; }
            set
            {
                if (_meseta != value)
                {
                    _meseta = value;
                    OnPropertyChanged("Meseta");
                }
            }
        }

        public int InventoryCount
        {
            get { return _inventory.Count; }
        }

        public void Refresh()
        {
            try
            {
                var playerIndex = _owner.Index;
                var inventory = Inventory;
                var oldInventoryCount = InventoryCount;

                Meseta = _model.GetPlayerMeseta(playerIndex);
                var newInventory = _model.GetPlayerInventory(playerIndex).ConvertAll<ItemViewModel>(item => (ItemViewModel)item);

                //Remove old items by throwing out all current inventory items that aren't in the new 
                //collection of inventory items returned from the model. Update any items that are 
                //already in the current inventory.
                var itemsToRemove = new List<ItemViewModel>();
                foreach (var item in inventory)
                {
                    var foundElement = newInventory.FirstOrDefault(element => element.Id == item.Id);
                    if (foundElement == null)
                        itemsToRemove.Add(item);
                    else
                        item.Update(foundElement);
                }
                foreach (var item in itemsToRemove)
                    inventory.Remove(item);

                //Add to the current inventory any items that are in the inventory returned by the 
                //model but not in the current inventory.
                foreach (var item in newInventory)
                {
                    if (inventory.FirstOrDefault(element => element.Id == item.Id) == null)
                        inventory.Add(item);
                }

                //Only raise a propertychanged event if the inventory count actually changed
                if (InventoryCount != oldInventoryCount)
                    OnPropertyChanged("InventoryCount");
            }
            catch (Exception) { } //Ignore all exceptions
        }
    }
}
